package www.xpu.edu.app.job;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import www.xpu.edu.app.Adapter_job3;
import www.xpu.edu.app.Job3;
import www.xpu.edu.app.Job3Bean;
import www.xpu.edu.app.R;
import www.xpu.edu.app.WorkBean;


public class Activity_job3 extends AppCompatActivity {
     //简历管理
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job3);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Button button1=(Button)findViewById(R.id.title_change);
        Button button2=(Button)findViewById(R.id.title_back);
        TextView text=findViewById(R.id.title_text);
        text.setText("简历管理");
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //设置此按钮不可见，但是依然占用着位置
        button1.setVisibility(View.INVISIBLE);

        SharedPreferences pref=getSharedPreferences("num",MODE_PRIVATE);
        SharedPreferences pref2=getSharedPreferences("job3",MODE_PRIVATE);
        //获取这两个值必须按照这个先后顺序写，不然会空引用异常
        String num=pref.getString("num","");
        //为什么用sout语句打印呢？？？日志猫里的东西太多了，还不如sout来的实在！！！【实际开发中这样搞会被开出的】
        //System.out.println("测试语句，我不知道到底有没有去除shared储存里面的值");
        System.out.println(num);
        sendRequest(num);

        String jobdata=pref2.getString("data","");
        System.out.println("数据->"+jobdata);


        /*Random rd=new Random();
        for (int i = 0; i < 20; i++) {
            list.add(new Job3("张三","西安工程大学",Integer.valueOf(2018+rd.nextInt(4))+"届毕业生"+Integer.valueOf(18+rd.nextInt(5))+"岁"+"本科"));
        }*/
        ArrayList<Job3> list2=new ArrayList<>();

        ArrayList<Job3Bean> list;
        Gson gson=new Gson();
        list=gson.fromJson(jobdata,new TypeToken<List<Job3Bean>>(){}.getType());
        if(list==null)
        {
            startActivity(new Intent(Activity_job3.this,Activity_job3.class));
        }
        for (int i = 0; i < (list != null ? list.size() : 0); i++) {
            list2.add(new Job3(list.get(i).getName(),list.get(i).getEdu(),String.valueOf(list.get(i).getGradtime()+"届毕业生 "+list.get(i).getAge()+"岁 "+list.get(i).getExp4())));
        }

        ListView listview=(ListView)findViewById(R.id.job3_list);
        listview.setAdapter(new Adapter_job3(Activity_job3.this,R.layout.resume_list_layout,list2));
        final ArrayList<Job3Bean>list3=list;
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     Intent intent=new Intent(Activity_job3.this,Activity_job_4.class);
                     ArrayList<String> list=new ArrayList<>();
                     list.add(list3.get(position).getName());
                     list.add(String.valueOf(list3.get(position).getGradtime()+"届毕业生 "+list3.get(position).getAge()+"岁 "+list3.get(position).getExp4()));
                     list.add(list3.get(position).getAdv());
                     list.add(list3.get(position).getExp1());
                     list.add(list3.get(position).getExp2());
                     list.add(list3.get(position).getExp3());
                     list.add(list3.get(position).getExp4());
                     list.add(list3.get(position).getIndex());
                     list.add(list3.get(position).getExp5());
                     //存储学生的num
                     list.add(list3.get(position).getNum());
                     list.add(String.valueOf(list3.get(position).getId()));
                     intent.putStringArrayListExtra("data",list);
                     startActivity(intent);
            }
        });

    }
    private void sendRequest(final String num)
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client=new OkHttpClient();
                    Request request=new Request.Builder()
                            /*http://192.168.43.188:8080/test2/resume*/
                            /*http://192.168.123.196:8080/test2/resume*/
                            //.url("http://192.168.43.188:8080/test2/resume")
                            .url("http://188.131.250.4:8080/xpu/select_resume?num="+num)
                            .build();
                    Response response=client.newCall(request).execute();
                    String responseData=response.body().string();
                    SharedPreferences.Editor editor=getSharedPreferences("job3",MODE_PRIVATE).edit();
                   // System.out.println("job3的内容【服务器端获取】->"+responseData);
                    editor.putString("data",responseData);
                    editor.apply();



                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
}
